//
//  RHMyRepairsCell.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/8/3.
//

#import "RHMyRepairsCell.h"
#import "QMUILabel+LHAdd.h"

@interface RHMyRepairsCell()
//设备名称
@property (nonatomic ,strong) QMUILabel *devName;
//设备编号
@property (nonatomic ,strong) QMUILabel *devNum;
//问题描述
@property (nonatomic ,strong) QMUILabel *troubleDesLab;
//报修时间
@property (nonatomic ,strong) QMUILabel *timeLab;

@end

@implementation RHMyRepairsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}
-(void)setFaultModel:(RHDeviceFaultModel *)faultModel
{
    _faultModel = faultModel;
    self.devName.text =       _faultModel.deviceName;
    self.devNum.text =        _faultModel.deviceId;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_faultModel.createTime/1000];
    self.timeLab.text =       [NSString stringWithFormat:@"%@: %@",@"上报时间".rh_localized,[formatter stringFromDate:date]];
    
    self.troubleDesLab.text = _faultModel.dev_description;
//    self.troubleDesLab.text = @"测试11111111111111111111111111111111111111111111";
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSInteger row = self.troubleDesLab.size.height/self.troubleDesLab.font.lineHeight;
        if(row ==1){
            self.troubleDesLab.textAlignment = NSTextAlignmentRight;
        }else{
            self.troubleDesLab.textAlignment = NSTextAlignmentLeft;
        }
    });
    
}


- (void)setupUI{
    
    [self setBackgroundColor:CFTabViewBgColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [bgView.layer setCornerRadius:8];
    [self.contentView addSubview:bgView];
    
    
    __block CGFloat labWidth = 0;
    NSMutableArray *array = [NSMutableArray array];
    NSArray *tittles = @[@"设备名称".rh_localized,@"设备ID".rh_localized,@"故障描述".rh_localized];
    [tittles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        QMUILabel *titleLab = [[QMUILabel alloc] init];
        [titleLab setTextAlignment:NSTextAlignmentLeft];
        [titleLab setFont:[UIFont systemFontOfSize:15]];
        [titleLab setTextColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.5]];
        [titleLab setText:[NSString stringWithFormat:@"%@:",obj]];
        [bgView addSubview:titleLab];
        [array addObject:titleLab];
        
    }];
    
    
    _devName = [[QMUILabel alloc] init];
    [_devName setTextAlignment:NSTextAlignmentRight];
    [_devName setFont:[UIFont systemFontOfSize:15]];
    [_devName setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_devName];
    
    
    _devNum = [[QMUILabel alloc] init];
    [_devNum setTextAlignment:NSTextAlignmentRight];
    [_devNum setFont:[UIFont systemFontOfSize:15]];
    [_devNum setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_devNum];
    
    
    _troubleDesLab = [[QMUILabel alloc] init];
    [_troubleDesLab setTextAlignment:NSTextAlignmentRight];
    [_troubleDesLab setFont:[UIFont systemFontOfSize:15]];
    _troubleDesLab.numberOfLines = 0;
    [_troubleDesLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_troubleDesLab];
    
    
    _timeLab = [[QMUILabel alloc] init];
    [_timeLab setTextAlignment:NSTextAlignmentLeft];
    [_timeLab setFont:[UIFont systemFontOfSize:15]];
    [_timeLab setTextColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.5]];
    [bgView addSubview:_timeLab];
    
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:CFCellLineColor];
    [bgView addSubview:lineV];


    kWeakSelf;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    [_devName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(200);
    }];
    
    [[array firstObject] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.bottom.equalTo(weakSelf.devName);
        make.width.mas_equalTo(100);
    }];
    
    [_devNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.devName.mas_bottom).offset(10);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(200);
    }];
    
    [[array objectAtIndex:1] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.bottom.equalTo(weakSelf.devNum);
        make.width.mas_equalTo(110);
    }];
    
    
    QMUILabel *lastLab = [array lastObject];
    [_troubleDesLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.devNum.mas_bottom).offset(10);
        make.right.mas_offset(-15);
//        make.height.mas_equalTo(100);
        make.left.equalTo(lastLab.mas_right).offset(15);
    }];
    
    [[array lastObject] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(weakSelf.troubleDesLab);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(120);
    }];
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(weakSelf.troubleDesLab.mas_bottom).offset(25);
        make.height.mas_equalTo(1);
    }];
    
    [_timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineV);
        make.top.equalTo(lineV.mas_bottom).offset(5);
        make.right.equalTo(lineV);
        make.height.mas_equalTo(44);
        make.bottom.mas_offset(-5);
    }];
    
    
    [_devName setText:@"Device A"];
    [_devNum setText:@"HY_0022"];
    [_troubleDesLab setText:@"troubleDes"];
    [_timeLab setText:@"Reporting time：2023-08-03"];
    
//    CGSize size = [_troubleDesLab getLableSizeWithMaxWidth:_troubleDesLab.frame.size.width];
//
//    [_troubleDesLab mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(MAX(30, size.height));
//    }];
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
